package com.yd.mvc.mobile.goodsController;

import com.yd.app.service.mobile.goodsService.MGoodsService;
import com.yd.app.service.mobile.goodsTypeService.MGoodsTypeService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.*;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.GoodsSearchParam;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-07 17:40
 * @desc
 **/
@RestController
@RequestMapping(value = "/mapi/mGoods")
public class MGoodsController extends BaseController{
    @Autowired
    private MGoodsService goodsService;
    @Autowired
    private MGoodsTypeService mGoodsTypeService;

    /**
     * 查询商品
     * @param goodsId
     */
    @RequestMapping(value = "/get/{goodsId}", method = RequestMethod.GET)
    public Goods getGoodsById(@PathVariable("goodsId")String goodsId,HttpServletRequest request) throws BusinessException {

        Goods goods=goodsService.getGoodsById(goodsId);
        HttpSession session = request.getSession();
        User u = (User) session.getAttribute("login_user");
        return goods;
    }

    /**
     * 商品信息列表
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel goodsList(@RequestBody GoodsSearchParam param, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize)throws Exception {
        PageModel pm=goodsService.goodsList(param,pageIndex,pageSize);

        return pm;
    }

    /**
     * 上滑加载
     * @param param
     * @param startIndex
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listSH/{startIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel goodsListSH(@RequestBody GoodsSearchParam param, @PathVariable("startIndex")int startIndex, @PathVariable("pageSize")int pageSize)throws Exception {
        PageModel pm=goodsService.goodsListSH(param,startIndex,pageSize);
        return pm;
    }

    /**
     * @Author: zy
     * @Desc: 获取所有照片
     * @Date:  2018/4/3
     * @Param
     */
    @RequestMapping(value = "/getAllPic/{goodId}", method = RequestMethod.GET)
    public List<Pic> getAllPic(@PathVariable("goodId")String goodId){
        List<Pic> list=new ArrayList<>();
        list=goodsService.getAllPic(goodId);
        return list;
    }

    /**
     * 商品列表页面初次加载
     * @param param
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listSH_init/{pId}/{pageSize}", method = RequestMethod.POST)
    public List<GStyleWithGoods> goodsListInit(@RequestBody GoodsSearchParam param,@PathVariable("pId")String pId,@PathVariable("pageSize")int pageSize)throws Exception {
        List<GStyleWithGoods> list=mGoodsTypeService.getGStyleWithGoodsList(pId);
        for(GStyleWithGoods gswg:list){
            param.setGstyle(gswg.getId());
            List<Goods>  goodsList=goodsService.goodsListSH_init(param,0,pageSize);
            gswg.setGoodsList(goodsList);
            List<PinXing> pxList=mGoodsTypeService.getPxByPid(gswg.getId());
            gswg.setPxList(pxList);
        }
        return list;
    }
}
